import axios from 'axios';
import {isFunction} from '@/utils/is';

let pendingMap = new Map();

export const getPendingUrl = (config) => [config.method, config.url].join('&');


export class AxiosCanceler{
  addPending(config){
    this.removePending(config);
    const url = getPendingUrl(config);
    config.cancelToken = config.cancelToken || new axios.CancelToken((cancel)=>{
      if(!pendingMap.has(url)){
          pendingMap.set(url,cancel)
      }
    })
  }

  removeAllPending(){
    pendingMap.forEach((cancel)=>{
      cancel && isFunction(cancel) && cancel();
    });
    pendingMap.clear();
  }

  removePending(config){
    const url = getPendingUrl(config);
    if(pendingMap.has(url)){
      const cancel = pendingMap.get(url);
      cancel && cancel(url)
      pendingMap.delete(url)
    }
  }

  reset() {
    pendingMap = new Map();
  }



}
